import { Service } from "typedi";

@Service()
export class ResultDataMiddlewares {
	private  _code!: number;
	private  _data!: Object;
	private  _message!: string;

	  get code(): number {
		return this._code;
	}

	  set code(value: number) {
		this._code = value;
	}

	  get data(): Object {
		return this._data;
	}

	  set data(value: Object) {
		this._data = value;
	}

	  get message():string{
		return this._message;
	}

	  set message(value:string){
		this._message = value;
	}


	/**
	 * 数据请求成功
	 * @param _data 需要返回给前端的数据
	 */
	public  success (_data:Object): Object {
		this.code = 200;
		this.data = _data;
		return {
			code: this.code,
			data: this.data,
			message:'请求成功'
		}
	}

	/**
	 * 数据请求失败
	 * @param action 请求对象
	 */
	 public  error (message: string): Object {
		this.code = 404;
		this.message = message;  // 错误信息
		return {
			code: this.code,
			data: '',
			message:this.message
		}
	}
}